import { getLogin } from "../../api/person";

Page({
  data: {
    visible: false,
    username: '',     
    password: '',     
    isLogin: false,
    userInfo: null,
    isLoading: false,   
    showPassword: false,
    tipVisible: false 
  },

  onLoad() {
    this.checkLoginStatus();
  },

  // 检查本地存储的登录状态
  checkLoginStatus() {
    const userInfo = wx.getStorageSync('userInfo');
    const isLogin = wx.getStorageSync('isLogin');
    
    if (userInfo && isLogin) {
      this.setData({
        isLogin: true,
        userInfo: userInfo
      });
    }
  },

  // 显示登录弹窗
  showModal() {
    this.setData({ visible: true });
  },

  // 隐藏登录弹窗
  hideModal() {
    this.setData({ visible: false });
  },

  handleUsernameInput(e) {
    let value = e.detail.value?.trim(); 
    if (value.length > 20) { 
      value = value.substring(0, 20);
    }
    this.setData({ username: value }); 
  },

  handlePasswordInput(e) {
    let value = e.detail.value;
    if (value.length > 20) { 
      value = value.substring(0, 20);
    }
    this.setData({ password: value });
  },


  togglePasswordVisibility() {
    this.setData({
      showPassword: !this.data.showPassword
    });
  },


  logout() {
    wx.removeStorageSync('userInfo');
    wx.removeStorageSync('isLogin');
    
    this.setData({
      isLogin: false,
      userInfo: null,
      username: '', 
      password: '' 
    });
    
    wx.showToast({
      title: '已退出登录',
      icon: 'none',
      duration: 1500
    });
  },

  login() {
 
    if (this.data.isLoading) return;

    const { username, password } = this.data;

    if (!username) {
      wx.showToast({ title: '请输入用户名', icon: 'none' });
      return;
    }
    if (!password) {
      wx.showToast({ title: '请输入密码', icon: 'none' });
      return;
    }
    this.setData({ isLoading: true });
    wx.showLoading({ title: '登录中...', mask: true });

    console.log('登录请求参数：', { username, passwordLength: password.length });

    getLogin(username, password)
      .then(res => {
        this.setData({ isLoading: false });
        wx.hideLoading();

        console.log('登录接口返回：', res);

        if (res.code === 0) { 
          wx.setStorageSync('userInfo', res.userInfo || {});
          wx.setStorageSync('isLogin', true);

          this.setData({
            isLogin: true,
            userInfo: res.userInfo || {},
            visible: false,
            username: '',   
            password: ''
          });

          wx.showToast({ title: '登录成功', icon: 'success' });

        } else { 
          wx.showToast({
            title: res.message || `登录失败（错误码：${res.code}）`,
            icon: 'none',
            duration: 2000
          });
        }
      })
      .catch(err => { 
        this.setData({ isLoading: false });
        wx.hideLoading();
        
        wx.showToast({
          title: err.message || '网络错误，请稍后重试',
          icon: 'none',
          duration: 2000
        });
        console.error('登录请求异常：', err);
      });
  },
  showTipModal() {
    this.setData({
      tipVisible: true
    });
  },
  hideTipModal() {
    this.setData({
      tipVisible: false
    });
  },
  toggleTipModal() {
    this.setData({
      tipVisible: !this.data.tipVisible
    });
  }
});